package dao.impl;

import dao.BaseDao;
import dao.BatteryDao;
import entity.Battery;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BatteryDaoImpl extends BaseDao implements BatteryDao {
    Battery battery = null;
    @Override
    public List<Battery> info() {
        conn = super.getConnection();
        List<Battery> list = new ArrayList<>();

        try{
            String sql  = "select * from powerbattery";
            
            pstmt = conn.prepareStatement(sql);

            rs = pstmt.executeQuery();

            while (rs.next()){
                battery = new Battery();
                battery.setId(rs.getInt("id"));
                battery.setCarName(rs.getString("carName"));
                battery.setCityName(rs.getString("cityName"));
                battery.setUserName(rs.getString("userName"));
                battery.setPressureValue(rs.getInt("pressureValue"));
                battery.setTime(rs.getString("time"));
                list.add(battery);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Battery selectAll(int id) {
        conn = super.getConnection();
        Battery battery = null;
        try{
            String sql  = "select * from powerbattery where id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            rs = pstmt.executeQuery();
            while (rs.next()){
                battery = new Battery();
                battery.setId(rs.getInt("id"));
                battery.setCarName(rs.getString("carName"));
                battery.setCityName(rs.getString("cityName"));
                battery.setUserName(rs.getString("userName"));
                battery.setPressureValue(rs.getInt("pressureValue"));
                battery.setTime(rs.getString("time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return battery;
    }

    @Override
    public int update(Battery battery) {
        conn = super.getConnection();
        int i = 0;
        try{
            String sql = "UPDATE `microblog`.`powerbattery` SET `carName` = ?, `cityName` = ?, `userName` = ?, `pressureValue` = ?, `time` = ? WHERE `id` = ?;";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,battery.getCarName());
            pstmt.setString(2,battery.getCityName());
            pstmt.setString(3,battery.getUserName());
            pstmt.setInt(4,battery.getPressureValue());
            pstmt.setString(5,battery.getTime());
            pstmt.setInt(6,battery.getId());
            i = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int delete(int id) {
        conn = super.getConnection();
        int i  = 0;
        try{
            String sql = "delete from powerbattery where id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            i = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
